/**
 *	Main.js - contains:
 *		- initial app setup functions
 *		- main app navigation function
**/

var Main = {
	
	debug: false,
	
	_pageStyle: 'inline-block',
	
	//init function
	init: function() {
		this._setupAllButtons()
		
		if (this.debug) {
			document.getElementById('LPuser').value = 'testlol'
			document.getElementById('LPgamelist').selectedIndex = 4
		}
		
		this.goTo('LoginPage')
		return
		
		var game = {
			board: { h: 5, w: 5 },
			state: [
				[0, 0, 0, 0, 0],
				[1, 0, 1, 0, 1],
				[0, 0, 0, 0, 0],
				[0, 0, 0, 0, 0],
				[0, 0, 0, 0, 0],
			],
			players: [ "bob", "dave" ],
			winConditions: [
				"player1InOneRowLose",
				"player1CannotTakeOpponentWin"
			]
		}
		var a = GameLogic.winChecker.check(game)
		console.log(a)
		
		return
		// debug: test mouse click
		this.clientInfo = { user: "bob" }
		Main.goTo("GamePage")
		
		Game.setUpGame({
			board: { h: 3, w: 3 },
			state: [
				[0, 0, 0],
				[0, 0, 0],
				[0, 0, 0],
			],
			players: [ "bob", "dave" ]
		})

		Game.draw()
	},
	
	//set up anchor buttons to flash on focus events
	_setupButtonFn: [
		function() { return false },
	],
	_setupAllButtons: function() {
		var a = document.getElementsByTagName('a'),
			regexp = /\s*button\s*/
		
		for (var i=0; i<a.length; i++) {
			if ( a[i].className.match(regexp) ) {
				this.setupButton(a[i])
			}
		}
	},
	
	//setup the anchor as a button
	setupButton: function(btn) {
		btn.onmousedown = this._setupButtonFn[0]
	},
	
	//logic required to go to page
	goTo: function(id) {
		if (typeof this._currentPage !== 'undefined') {
			this._currentPage.style.display = 'none'
		}
		
		this._currentPage = document.getElementById(id)
		this._currentPage.style.display = this._pageStyle
		
		if ( this._goToActions[id] ) {
			this._goToActions[id]()
		}
	},
	
	//special actions to take after goto
	_goToActions: {
		GamePage: function() {
			Game.init()		// game div must be visible in order for init to work.
		}
	}
	
}